﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using KorleBu.Application_Classes;

namespace KorleBu
{
    public partial class Users : System.Web.UI.Page
    {
        #region Class State Variables

        /// <summary>
        /// Dataset object, used for housing user method calls results.
        /// </summary>
        DataSet ds = new DataSet();
        /// <summary>
        /// Presentation object that accesses the presentation object classes methods.
        /// Used for accessing business methods for the user page.
        /// </summary>
        Presentation presentation = new Presentation();
        /// <summary>
        /// Represents the salt and password hash that was created using the createHashedPassword().
        /// </summary>
        //string[] hashWithSalt;
        #endregion

        #region Page Load / Tab Control Methods
        /// <summary>
        /// Page load method, ran at the beginning of the users page startup load.
        /// Checks to see if the user actually logged in and if they have admin privileges, 
        /// if not they are redirected to the login page or the home page depending on the condition.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMasterError.Text = "";

            //if the user didn't log in, but browsed to this page redirect them to the login page.
            if (GetSessionID() == null || GetSessionID() == "")
            {
                Response.Redirect("~/Login.aspx", true);
            }
            //if the user doesn't have admin privileges send them back to the home page
            if (GetIsAdmin() == false)
            {
                Response.Redirect("~/Home.aspx", true);
            }
            lblLoggedInUser.Text = "You are logged in as " + GetFirstName() + " " + GetLastName();
            if (!IsPostBack)
            {
                TabContainer_ActiveTabChanged(TabContainer, null);  //initializes the tab index to the starting index

                //show a message for pending tasks if the given user has tasks to do.  
                //This should only apply to store managers, general managers, CEOs, and Procurement
                if (GetIsStoreManager())
                {
                    ds = presentation.GetPendingRequests(GetStoreID());
                    if (ds.Tables.Count > 0) // If the method returned successfully
                    {
                        if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                        {
                            //lblSideBarContent.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                            lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                        }
                        else
                        {
                            //lblSideBarContent.Text = "**You have pending satellite store requests to handle.**";
                        }
                    }
                }
                else if (GetIsGeneralManager())
                {
                    ds = presentation.GetRequisitionsByPendingStatus();
                    if (ds.Tables.Count > 0) // If the method returned successfully
                    {
                        if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                        {
                            //lblSideBarContent.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                            lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                        }
                        else
                        {
                            //lblSideBarContent.Text = "**You have pending requisitions to handle.**";
                        }
                    }
                }
                else if (GetIsCEO())
                {
                    ds = presentation.GetRequisitionsByGMStatus();
                    if (ds.Tables.Count > 0) // If the method returned successfully
                    {
                        if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                        {
                            //lblSideBarContent.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                            lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                        }
                        else
                        {
                            //lblSideBarContent.Text = "**You have pending requisitions to handle.**";
                        }
                    }
                }
                else if (GetIsProcurement())
                {
                    ds = presentation.GetRequisitionsByCEOStatus();
                    if (ds.Tables.Count > 0) // If the method returned successfully
                    {
                        if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                        {
                            //lblSideBarContent.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                            lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                        }
                        else
                        {
                            //lblSideBarContent.Text = "**You have pending requisition orders to handle.**";
                        }
                    }
                }

                //load up the store names drop down for the create/modify tabs under the field set User Roles
                populateCStore_MStoreDropDowns();

                divCStore.Style.Value = "visibility:hidden;";
                divMStore.Style.Value = "visibility:hidden;";

                //load up the user account status drop down for the modified tab under the field set User Account Status
                ds = presentation.GetAvailableUserStatuses();

                ddlMStatus.DataSource = ds.Tables[1];
                ddlMStatus.DataTextField = "StatusName";
                ddlMStatus.DataValueField = "statusID";
                ddlMStatus.DataBind();

                //load up drop downs for creating stores
                ds = presentation.GetStoreTypes();
                ddlStoreTypes.DataSource = ds.Tables[1];
                ddlStoreTypes.DataTextField = "storeTypeName";
                ddlStoreTypes.DataValueField = "storeTypeID";
                ddlStoreTypes.DataBind();

                ds = presentation.GetActiveSubUnits();
                ddlCSubUnits.DataSource = ds.Tables[1];
                ddlCSubUnits.DataTextField = "subunitName";
                ddlCSubUnits.DataValueField = "subunitID";
                ddlCSubUnits.DataBind();

                ds = presentation.GetActiveCentralStores();
                ddlCStores.DataSource = ds.Tables[1];
                ddlCStores.DataTextField = "storeName";
                ddlCStores.DataValueField = "storeID";
                ddlCStores.DataBind();

                //populate store/sub unit/store unit drop downs

                ds = presentation.GetSubUnits();
                ddlSubUnitList.DataSource = ds.Tables[1];
                ddlSubUnitList.DataTextField = "subUnitName";
                ddlSubUnitList.DataValueField = "subUnitID";
                ddlSubUnitList.DataBind();

                ds = presentation.GetAvailableSubUnitStatuses();
                ddlMSubUnitStatuses.DataSource = ds.Tables[1];
                ddlMSubUnitStatuses.DataTextField = "StatusName";
                ddlMSubUnitStatuses.DataValueField = "statusID";
                ddlMSubUnitStatuses.DataBind();

                ds = presentation.GetAllStores();
                ddlStoreList.DataSource = ds.Tables[1];
                ddlStoreList.DataTextField = "storeName";
                ddlStoreList.DataValueField = "storeID";
                ddlStoreList.DataBind();

                ds = presentation.GetStoreTypes();
                ddlMStoreTypes.DataSource = ds.Tables[1];
                ddlMStoreTypes.DataTextField = "storeTypeName";
                ddlMStoreTypes.DataValueField = "storeTypeID";
                ddlMStoreTypes.DataBind();

                ds = presentation.GetAvailableStoreStatuses();
                ddlMStoreStatuses.DataSource = ds.Tables[1];
                ddlMStoreStatuses.DataTextField = "StatusName";
                ddlMStoreStatuses.DataValueField = "statusID";
                ddlMStoreStatuses.DataBind();

                ds = presentation.GetActiveSubUnits();
                ddlMSubUnits.DataSource = ds.Tables[1];
                ddlMSubUnits.DataTextField = "subunitName";
                ddlMSubUnits.DataValueField = "subunitID";
                ddlMSubUnits.DataBind();

                ds = presentation.GetActiveCentralStores();
                ddlMStores.DataSource = ds.Tables[1];
                ddlMStores.DataTextField = "storeName";
                ddlMStores.DataValueField = "storeID";
                ddlMStores.DataBind();

                ds = presentation.GetAvailableStoreUnitStatuses();
                ddlMStoreUnitStatuses.DataSource = ds.Tables[1];
                ddlMStoreUnitStatuses.DataTextField = "StatusName";
                ddlMStoreUnitStatuses.DataValueField = "statusID";
                ddlMStoreUnitStatuses.DataBind();

                //Disable modify store section form fields
                txtMStoreName.Enabled = false;
                ddlMStoreTypes.Enabled = false;
                ddlMStoreStatuses.Enabled = false;
                btnSubmitStoreChanges.Enabled = false;

                txtMSubUnitName.Enabled = false;
                ddlMSubUnitStatuses.Enabled = false;
                btnSubmitSubUnitChanges.Enabled = false;

                ddlMStoreUnitStatuses.Enabled = false;
                btnSubmitStoreUnitChanges.Enabled = false;

                //clear out any success or error messages for that tab form and their form fields.
                lblCError.Text = "";
                lblCreateSuccess.Text = "";
                lblMError.Text = "";
                lblModifySuccess.Text = "";
                lblCStoreSuccess.Text = "";
                lblCStoreError.Text = "";
                lblMStoreSuccess.Text = "";
                lblMStoreError.Text = "";
                lblMasterError.Text = "";
            }
        }
        /// <summary>
        /// Method handles the changing of tabs.  The far left tab starts at index 0.
        /// Place any code in the designated tab that needs to be changed dynamically each time a the tab is clicked.
        /// Clears out error and success messages when a new tab is clicked.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void TabContainer_ActiveTabChanged(object sender, EventArgs e)
        {
            try
            {
                if (TabContainer.ActiveTabIndex == 0) { lblCError.Text = ""; lblCreateSuccess.Text = ""; }

                if (TabContainer.ActiveTabIndex == 1) { lblMError.Text = ""; lblModifySuccess.Text = ""; }

                if (TabContainer.ActiveTabIndex == 2) { lblCStoreError.Text = ""; lblCStoreSuccess.Text = ""; }

                if (TabContainer.ActiveTabIndex == 3) { lblMStoreError.Text = ""; lblMStoreSuccess.Text = ""; }
            }
            catch (Exception ex)
            {
                lblMasterError.Text = ex.Message;
            }
            finally
            {

            }
        }
        #endregion

        #region User Methods
        /// <summary>
        /// Method handles when the selected option from the Search by drop down is changed.
        /// Depending on what is chosen determines what will be populated for the roles, statuses drop down, and/or the user list drop down. 
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void ddlSearchBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for drop down selection.
            lblModifySuccess.Text = "";
            lblMError.Text = "";

            //if search by user name is selected, enable the user list drop down and populate it's select options pulling 
            //all user's first and last names in alphabetical order.  
            if (ddlSearchBy.SelectedItem.Text == "Person's Name")
            {
                //disable the roles and statuses list drop downs just in case they were selected prior.   
                //This will eliminate any issues with flow of drop down selections.
                ddlRoles.Enabled = false;
                ddlStatuses.Enabled = false;

                ds = presentation.GetAllUsers();

                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                    }
                    else
                    {
                        ddlUserList.Enabled = true;
                        ddlUserList.DataSource = ds.Tables[1];
                        ddlUserList.DataTextField = "name";
                        ddlUserList.DataValueField = "userID";
                        ddlUserList.DataBind();
                        btnSelectUser.Enabled = true;
                    }
                }
                else
                {
                    lblMasterError.Text = "Sorry, the system couldn't find any users.";
                }
            }
            //if search by roles is selected, enable the roles_statuses drop down and populate it's select options pulling
            //all the roles from the database.
            else if (ddlSearchBy.SelectedItem.Text == "User Roles")
            {
                //disable the users and statuses list drop downs just in case they were selected prior.  
                //This will eliminate any issues with flow of drop down selections.
                ddlUserList.Enabled = false;
                ddlStatuses.Enabled = false;
                ddlRoles.Enabled = true;
                btnSelectUser.Enabled = false;
            }
            //if search by user account status, enable the roles_statuses drop down and populate it's select options pulling
            //all the user account statuses from the database.
            else if (ddlSearchBy.SelectedItem.Text == "User Account Status")
            {
                //disable the users and roles list drop downs just in case they were selected prior.  
                //This will eliminate any issues with flow of drop down selections.
                ddlUserList.Enabled = false;
                ddlRoles.Enabled = false;
                btnSelectUser.Enabled = false;

                ds = presentation.GetAvailableUserStatuses();

                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                    }
                    else
                    {
                        ddlStatuses.Enabled = true;
                        ddlStatuses.DataSource = ds.Tables[1];
                        ddlStatuses.DataTextField = "StatusName";
                        ddlStatuses.DataValueField = "statusID";
                        ddlStatuses.DataBind();
                    }
                }
                else
                {
                    lblMasterError.Text = "Sorry, the system couldn't find any user account statuses.";
                }
            }
        }
        /// <summary>
        /// Method handles when the selected option from the Roles drop down is changed.
        /// Depending on the role that is chosen, the user list drop down of user names will be populated accordingly.
        /// Method also enables the user list drop down.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void ddlRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for drop down selection.
            lblModifySuccess.Text = "";
            lblMError.Text = "";

            //depending on what was selected from the search by drop down, choose the correct switch statement to use.
            //2 - Search by Roles
            //3 - Search by User Account Status
            if (ddlSearchBy.SelectedValue.Equals("2"))
            {
                switch (ddlRoles.SelectedValue)
                {
                    case "0":
                        break;
                    //if searching Admin role, pull all user first and last names that have admin privileges.  
                    //Populate the user list drop down with the returned results.
                    case "1":
                        ds = presentation.GetUsersByRole(true, false, false, false, false, false, false, false);

                        if (ds.Tables.Count > 0) // If the method returned successfully
                        {
                            if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                            {
                                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                            }
                            else
                            {
                                ddlUserList.Enabled = true;
                                ddlUserList.DataSource = ds.Tables[1];
                                ddlUserList.DataTextField = "name";
                                ddlUserList.DataValueField = "userID";
                                ddlUserList.DataBind();
                                btnSelectUser.Enabled = true;
                            }
                        }
                        else
                        {
                            lblMasterError.Text = "Sorry, the system couldn't find users according to the selection.";
                        }
                        break;
                    //if searching General Manager role, pull all user first and last names that have admin privileges.  
                    //Populate the user list drop down with the returned results.
                    case "2":
                        ds = presentation.GetUsersByRole(false, true, false, false, false, false, false, false);

                        if (ds.Tables.Count > 0) // If the method returned successfully
                        {
                            if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                            {
                                lblMError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                            }
                            else
                            {
                                ddlUserList.Enabled = true;
                                ddlUserList.DataSource = ds.Tables[1];
                                ddlUserList.DataTextField = "name";
                                ddlUserList.DataValueField = "userID";
                                ddlUserList.DataBind();
                                btnSelectUser.Enabled = true;
                            }
                        }
                        else
                        {
                            lblMasterError.Text = "Sorry, the system couldn't find users according to the selection.";
                        }
                        break;
                    //if searching Store Manager role, pull all user first and last names that have admin privileges.  
                    //Populate the user list drop down with the returned results.
                    case "3":
                        ds = presentation.GetUsersByRole(false, false, true, false, false, false, false, false);

                        if (ds.Tables.Count > 0) // If the method returned successfully
                        {
                            if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                            {
                                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                            }
                            else
                            {
                                ddlUserList.Enabled = true;
                                ddlUserList.DataSource = ds.Tables[1];
                                ddlUserList.DataTextField = "name";
                                ddlUserList.DataValueField = "userID";
                                ddlUserList.DataBind();
                                btnSelectUser.Enabled = true;
                            }
                        }
                        else
                        {
                            lblMasterError.Text = "Sorry, the system couldn't find users according to the selection.";
                        }
                        break;
                    //if searching Store Keeper role, pull all user first and last names that have admin privileges.  
                    //Populate the user list drop down with the returned results.
                    /*case "4":
                        ds = presentation.GetUsersByRole(false, false, false, true, false, false, false, false);

                        if (ds.Tables.Count > 0) // If the method returned successfully
                        {
                            if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                            {
                                lblMError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                            }
                            else
                            {
                                ddlUserList.Enabled = true;
                                ddlUserList.DataSource = ds.Tables[1];
                                ddlUserList.DataTextField = "name";
                                ddlUserList.DataValueField = "userID";
                                ddlUserList.DataBind();
                                btnSelectUser.Enabled = true; 
                            }
                        }
                        else
                        {
                            lblMError.Text = "Sorry, the system couldn't find users according to the selection.";
                        }
                        break;*/
                    //if searching Subunit Clerk role, pull all user first and last names that have admin privileges.  
                    //Populate the user list drop down with the returned results.
                    case "5":
                        ds = presentation.GetUsersByRole(false, false, false, false, true, false, false, false);

                        if (ds.Tables.Count > 0) // If the method returned successfully
                        {
                            if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                            {
                                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                            }
                            else
                            {
                                ddlUserList.Enabled = true;
                                ddlUserList.DataSource = ds.Tables[1];
                                ddlUserList.DataTextField = "name";
                                ddlUserList.DataValueField = "userID";
                                ddlUserList.DataBind();
                                btnSelectUser.Enabled = true;
                            }
                        }
                        else
                        {
                            lblMasterError.Text = "Sorry, the system couldn't find users according to the selection.";
                        }
                        break;
                    //if searching CEO role, pull all user first and last names that have admin privileges.  
                    //Populate the user list drop down with the returned results.
                    case "6":
                        ds = presentation.GetUsersByRole(false, false, false, false, false, true, false, false);

                        if (ds.Tables.Count > 0) // If the method returned successfully
                        {
                            if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                            {
                                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                            }
                            else
                            {
                                ddlUserList.Enabled = true;
                                ddlUserList.DataSource = ds.Tables[1];
                                ddlUserList.DataTextField = "name";
                                ddlUserList.DataValueField = "userID";
                                ddlUserList.DataBind();
                                btnSelectUser.Enabled = true;
                            }
                        }
                        else
                        {
                            lblMasterError.Text = "Sorry, the system couldn't find users according to the selection.";
                        }
                        break;
                    //if searching Accounting role, pull all user first and last names that have admin privileges.  
                    //Populate the user list drop down with the returned results.
                    /*case "7":
                        ds = presentation.GetUsersByRole(false, false, false, false, false, false, true, false);

                        if (ds.Tables.Count > 0) // If the method returned successfully
                        {
                            if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                            {
                                lblMError.Text = ds.Tables[0].Rows[0][0].ToString(); // Error message
                            }
                            else
                            {
                                ddlUserList.Enabled = true;
                                ddlUserList.DataSource = ds.Tables[1];
                                ddlUserList.DataTextField = "name";
                                ddlUserList.DataValueField = "userID";
                                ddlUserList.DataBind();
                                btnSelectUser.Enabled = true;
                            }
                        }
                        else
                        {
                            lblMError.Text = "Sorry, the system couldn't find users according to the selection.";
                        }
                        break;*/
                    //if searching Procurement role, pull all user first and last names that have admin privileges.  
                    //Populate the user list drop down with the returned results.
                    case "8":
                        ds = presentation.GetUsersByRole(false, false, false, false, false, false, false, true);

                        if (ds.Tables.Count > 0) // If the method returned successfully
                        {
                            if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                            {
                                lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                            }
                            else
                            {
                                ddlUserList.Enabled = true;
                                ddlUserList.DataSource = ds.Tables[1];
                                ddlUserList.DataTextField = "name";
                                ddlUserList.DataValueField = "userID";
                                ddlUserList.DataBind();
                                btnSelectUser.Enabled = true;
                            }
                        }
                        else
                        {
                            lblMasterError.Text = "Sorry, the system couldn't find users according to the selection.";
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        /// <summary>
        /// Method handles when the selected option from the Statuses drop down is changed.
        /// Depending on the status that is chosen, the user list drop down of user names will be populated accordingly.
        /// Method also enables the user list drop down.
        /// Enables the select button when the user drop down list populates correctly.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void ddlStatuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for drop down selection.
            lblModifySuccess.Text = "";
            lblMError.Text = "";

            int statusId = Convert.ToInt32(ddlStatuses.SelectedItem.Value);
            ds = presentation.GetUsersByStatusID(statusId);

            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                //If searching by status of user accounts, pull all user first and last names according to user's choice.  
                //Populate the user list drop down with the returned results.
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    ddlUserList.Enabled = true;
                    ddlUserList.DataSource = ds.Tables[1];
                    ddlUserList.DataTextField = "name";
                    ddlUserList.DataValueField = "userID";
                    ddlUserList.DataBind();
                    btnSelectUser.Enabled = true;
                }
            }
            else
            {
                lblMasterError.Text = "Sorry, the system couldn't find users according to the selection.";
            }
        }
        /// <summary>
        /// Method handles the selection of a searched user from the modify user tab.
        /// Populates the modify user's fields depending on the given selection of the user.
        /// </summary>
        //Modified by Adam Venezia 10-16-12; ddlMStore was not storing the right values when subunit clerk was to be modified.
        //Fixing this included moving the call to populateCStore_MStoreDropDowns(); to be above where interfacing with the ddl was.
        //Commented the same call from lower in the code.
        protected void btnSelectUser_Click(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for drop down selection.
            lblModifySuccess.Text = "";
            lblMError.Text = "";

            //Try to pull the selected user's account information from the database.
            ds = presentation.GetUserByID(Convert.ToInt32(ddlUserList.SelectedItem.Value));
            //counter any errors if any arise, otherwise populate modify user form fields according to returned results.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    //Populate the "Modify User" form fields with the database results
                    hfMUserId.Value = ds.Tables[1].Rows[0][0].ToString();
                    txtMFirstName.Text = ds.Tables[1].Rows[0][1].ToString();
                    txtMLastName.Text = ds.Tables[1].Rows[0][2].ToString();
                    txtMUserName.Text = ds.Tables[1].Rows[0][3].ToString();
                    hfMOldPassword.Value = ds.Tables[1].Rows[0][4].ToString();
                    txtMPasswordHint.Text = ds.Tables[1].Rows[0][5].ToString();
                    ddlMStatus.SelectedValue = ds.Tables[1].Rows[0][6].ToString();
                    populateCStore_MStoreDropDowns();
                    if (!String.IsNullOrEmpty(ds.Tables[1].Rows[0][7].ToString()))
                    {
                        ddlMStore.SelectedValue = ds.Tables[1].Rows[0][7].ToString();
                    }
                    else
                        ddlMStore.SelectedIndex = 0;
                    chkbxMAdmin.Checked = Convert.ToBoolean(ds.Tables[1].Rows[0][8].ToString());
                    chkbxMGenMngr.Checked = Convert.ToBoolean(ds.Tables[1].Rows[0][9].ToString());
                    if (Convert.ToBoolean(ds.Tables[1].Rows[0][10].ToString()) || Convert.ToBoolean(ds.Tables[1].Rows[0][12].ToString()))
                    {
                        divMStore.Style.Value = "visibility:visible";
                        
                    }
                    else
                    {
                        divMStore.Style.Value = "visibility:hidden";
                        ddlMStore.Enabled = false;
                    }
                    chkbxMStoreMngr.Checked = Convert.ToBoolean(ds.Tables[1].Rows[0][10].ToString());
                    //chkbxMStoreKpr.Checked = Convert.ToBoolean(ds.Tables[1].Rows[0][11].ToString());
                    chkbxMSubunitClrk.Checked = Convert.ToBoolean(ds.Tables[1].Rows[0][12].ToString());
                    chkbxMCeo.Checked = Convert.ToBoolean(ds.Tables[1].Rows[0][13].ToString());
                    //chkbxMAccounting.Checked = Convert.ToBoolean(ds.Tables[1].Rows[0][14].ToString());
                    chkbxMProcurement.Checked = Convert.ToBoolean(ds.Tables[1].Rows[0][15].ToString());
                    //txtMEmail.Text = ds.Tables[1].Rows[0][16].ToString();

                    //This needed to be called earlier on.
                    //populateCStore_MStoreDropDowns();

                    //Enable modify user section form fields
                    txtMFirstName.Enabled = true;
                    txtMLastName.Enabled = true;
                    txtMUserName.Enabled = true;
                    txtMPassword.Enabled = true;
                    txtMPasswordConfirm.Enabled = true;
                    txtMPasswordHint.Enabled = true;
                    ddlMStatus.Enabled = true;
                    ddlMStore.Enabled = true;
                    chkbxMAdmin.Enabled = true;
                    chkbxMGenMngr.Enabled = true;
                    chkbxMStoreMngr.Enabled = true;
                    //chkbxMStoreKpr.Enabled = true;
                    chkbxMSubunitClrk.Enabled = true;
                    chkbxMCeo.Enabled = true;
                    //chkbxMAccounting.Enabled = true;
                    chkbxMProcurement.Enabled = true;
                    //txtMEmail.Enabled = true;
                    btnSavePassword.Enabled = true;
                    btnSubmitChanges.Enabled = true;
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not find the user."; // If the Data Tier cannot be reached
        }
        /// <summary>
        /// Method handles the 'Save Password' button, takes the new password and changes the current password to the new one.
        /// Error message will be given if unsuccessful, success message will be given if all went through.
        /// 'New Password' label, 'Password' text box, and 'Cancel' button is hidden if successful.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSavePassword_Click(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for save password button results.
            lblModifySuccess.Text = "";
            lblMError.Text = "";

            // Check to see if the password and confirm password fields are the same
            if (txtMPassword.Text != txtMPasswordConfirm.Text)
            {
                lblMasterError.Text = "Passwords do not match";
                return;
            }

            int uId;  //houses the parsed out userId from the hidden label

            //try to update the user password, handle errors and issues as they come.
            if (Int32.TryParse(hfMUserId.Value, out uId))
            {
                //create hashed password and send in the salt and hashed password to the database.
                //if (!String.IsNullOrEmpty(txtMPassword.Text))
                //{
                //hashWithSalt = PasswordHash.createHashedPassword(txtMPassword.Text);
                ds = presentation.UpdatePassword(uId, hfMOldPassword.Value, txtMPassword.Text, "");

                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred during stored procedure
                    {
                        lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString(); // Show error message
                    }
                    else
                    {
                        //if successful state so, otherwise give error message.
                        if (Convert.ToBoolean(ds.Tables[1].Rows[0][0].ToString()))
                        {
                            lblModifySuccess.Text = "Password changed successfully.";
                            hfMOldPassword.Value = txtMPassword.Text;
                        }
                        else
                            lblMasterError.Text = "Password did not successfully change.";
                    }
                }
                else
                    lblMasterError.Text = "Sorry, the system could not change the password."; // If the Data Tier cannot be reached
                //}
                //else
                //    lblMError.Text = "The Password field must not be blank, please try again.";     //If the password text box was empty.
            }
            else
                lblMasterError.Text = "Please, select a user before trying to change a password.";  //if a user clicks save password without a user being selected, show an error.
        }
        /// <summary>
        /// Method handles the saving for the create user tab.  Method is called when the save button is clicked.
        /// If the user can't be successfully added to the database, an appropriate error message will be displayed.
        /// Otherwise a success message will be shown, and form fields will be reset to their initial state.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Clear out the error and success labels before attempting the database changes.
            lblCError.Text = "";
            lblCreateSuccess.Text = "";

            // Check to see if the password and confirm password fields are the same
            if (txtCPassword.Text != txtCPasswordConfirm.Text)
            {
                lblCError.Text = "Passwords do not match";
                return;
            }

            int storeID = -1;

            if(chkbxCStoreMngr.Checked || chkbxCSubunitClrk.Checked)
            {
                storeID = Convert.ToInt32(ddlCStore.SelectedValue);
            }

            //if (!String.IsNullOrEmpty(txtCPassword.Text))
            //{

                //create hashed password and send in the salt and hashed password to the database.
                //hashWithSalt = PasswordHash.createHashedPassword(txtCPassword.Text);

                //try to add the user
                /**Note accounting role and store keeper role is temporarily not being supported, this is why they are hard coded false values for them in the create user method.**/
                ds = presentation.AddUser(txtCFirstName.Text, txtCLastName.Text, txtCUserName.Text, txtCPassword.Text, txtCPasswordHint.Text, storeID, chkbxCAdmin.Checked, chkbxCGenMngr.Checked, chkbxCStoreMngr.Checked, false, chkbxCSubunitClrk.Checked, chkbxCCeo.Checked, false, chkbxCProcurement.Checked, "");

                //counter any errors if any arise, otherwise create the user and reset the 'Create User' form fields to their initial state.
                if (ds.Tables.Count > 0) // If the method returned successfully
                {
                    if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                    {
                        lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                    }
                    else
                    {
                        if (Convert.ToBoolean(ds.Tables[1].Rows[0][0].ToString()))
                        {
                            lblCreateSuccess.Text = "User \"" + txtCUserName.Text + "\" added successfully.";
                            //Reset 'Create User' form elements
                            resetForm("create");

                            //Reset the 'Modify User' form fields so that the newly created user is taken into account
                            //this is already called by resetForm("create");
                            //resetForm("modify");
                        }
                        else
                            lblMasterError.Text = "Sorry, the user could not be added.";
                    }
                }
                else
                    lblMasterError.Text = "Sorry, the system could not add the user."; // If the Data Tier cannot be reached
            //}
            //else
            //    lblCError.Text = "The Password field must not be blank, please try again.";     //If the create user's password text box is left empty.
        }
        /// <summary>
        /// Method handles the showing/hiding of the store name drop down list depending 
        /// on what tab the store roles' checkboxes were clicked.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void storeCheckBoxes_OnCheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkbox = (CheckBox)sender;

            if (checkbox.ID == "chkbxMStoreMngr")
            {
                chkbxMSubunitClrk.Checked = false;
                chkbxMGenMngr.Checked = false;
                chkbxMProcurement.Checked = false;
                chkbxMCeo.Checked = false;

            }
            else if (checkbox.ID == "chkbxMSubunitClrk")
            {
                chkbxMStoreMngr.Checked = false;
                chkbxMGenMngr.Checked = false;
                chkbxMProcurement.Checked = false;
                chkbxMCeo.Checked = false;
            }
            else if (checkbox.ID == "chkbxMGenMngr")
            {
                chkbxMStoreMngr.Checked = false;
                chkbxMSubunitClrk.Checked = false;
                chkbxMProcurement.Checked = false;
                chkbxMCeo.Checked = false;
            }
            else if (checkbox.ID == "chkbxMProcurement")
            {
                chkbxMStoreMngr.Checked = false;
                chkbxMSubunitClrk.Checked = false;
                chkbxMGenMngr.Checked = false;
                chkbxMCeo.Checked = false;
            }
            else if (checkbox.ID == "chkbxMCeo")
            {
                chkbxMStoreMngr.Checked = false;
                chkbxMSubunitClrk.Checked = false;
                chkbxMGenMngr.Checked = false;
                chkbxMProcurement.Checked = false;
            }

            if (checkbox.ID == "chkbxCStoreMngr")
            {
                chkbxCSubunitClrk.Checked = false;
                chkbxCGenMngr.Checked = false;
                chkbxCProcurement.Checked = false;
                chkbxCCeo.Checked = false;
            }
            else if (checkbox.ID == "chkbxCSubunitClrk")
            {
                chkbxCStoreMngr.Checked = false;
                chkbxCGenMngr.Checked = false;
                chkbxCProcurement.Checked = false;
                chkbxCCeo.Checked = false;
            }
            else if (checkbox.ID == "chkbxCGenMngr")
            {
                chkbxCStoreMngr.Checked = false;
                chkbxCSubunitClrk.Checked = false;
                chkbxCProcurement.Checked = false;
                chkbxCCeo.Checked = false;
            }
            else if (checkbox.ID == "chkbxCProcurement")
            {
                chkbxCStoreMngr.Checked = false;
                chkbxCSubunitClrk.Checked = false;
                chkbxCGenMngr.Checked = false;
                chkbxCCeo.Checked = false;
            }
            else if (checkbox.ID == "chkbxCCeo")
            {
                chkbxCStoreMngr.Checked = false;
                chkbxCSubunitClrk.Checked = false;
                chkbxCGenMngr.Checked = false;
                chkbxCProcurement.Checked = false;
            }

            //If the check box is on the modified tab, make the modified store drop down visible, otherwise do it for the create tab.
            if (checkbox.Checked == true && (checkbox.ID == "chkbxMStoreMngr" || checkbox.ID == "chkbxMSubunitClrk" || checkbox.ID == "chkbxCStoreMngr" || checkbox.ID == "chkbxCSubunitClrk"))
            {
                populateCStore_MStoreDropDowns();
                if (checkbox.ID.StartsWith("chkbxM"))
                {
                    divMStore.Style.Value = "visibility:visible;";
                }
                else
                {
                    divCStore.Style.Value = "visibility:visible;";
                }
            }
            else
            {
                if (checkbox.ID.StartsWith("chkbxM"))
                {
                    if (chkbxMStoreMngr.Checked == false && chkbxMSubunitClrk.Checked == false)
                        divMStore.Style.Value = "visibility:hidden;";
                }
                else
                {
                    if (chkbxCStoreMngr.Checked == false && chkbxCSubunitClrk.Checked == false)
                        divCStore.Style.Value = "visibility:hidden;";
                }
            }
        }
        /// <summary>
        /// Method handles the saving for the Modify user tab.  Method is called when the 'Submit Changes' button is clicked.
        /// If the user account can't be successfully modified in the database, an appropriate error message will be displayed.
        /// Otherwise a success message will be shown, and form fields will be reset to their initial state.
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">EventArgs</param>
        protected void btnSubmitChanges_Click(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for database attempted changes.
            lblModifySuccess.Text = "";
            lblMError.Text = "";

            int storeID = -1;

            if (chkbxMStoreMngr.Checked || chkbxMSubunitClrk.Checked)
            {
                storeID = Convert.ToInt32(ddlCStore.SelectedValue);
            }

            //try to modify/update the user's account
            /**Note accounting role and store keeper role is temporarily not being supported, this is why they are hard coded false values for them in the modify user method.**/
            ds = presentation.ModifyUser(Convert.ToInt32(hfMUserId.Value), txtMFirstName.Text, txtMLastName.Text, txtMUserName.Text, txtMPasswordHint.Text, Convert.ToInt32(ddlMStatus.SelectedItem.Value), storeID, chkbxMAdmin.Checked, chkbxMGenMngr.Checked, chkbxMStoreMngr.Checked, false, chkbxMSubunitClrk.Checked, chkbxMCeo.Checked, false, chkbxMProcurement.Checked);

            //counter any errors if any arise, otherwise modify/update the user's account information and reset the 'Modify User' form fields to their initial state.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    if (Convert.ToBoolean(ds.Tables[1].Rows[0][0].ToString()))
                    {
                        lblModifySuccess.Text = "User \"" + txtMUserName.Text + "\" updated successfully.";
                        //Reset 'Modify User' form fields
                        resetForm("modify");
                    }
                    else
                        lblMasterError.Text = "Sorry, the user could not be updated.";
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not update the user."; // If the Data Tier cannot be reached
        }
        #endregion

        #region Shared Methods
        /// <summary>
        /// Method populates the hidden select pertaining 
        /// store drop downs for the create and modify user sections.
        /// </summary>
        //Modified by Adam Venezia 10-16-12; Created a local DataSet, called local, because using a class-
        //dataset (ds) is idiotic and overwrote the dataset in other methods when they weren't done with it yet.
        public void populateCStore_MStoreDropDowns()
        {
            //load up the store names drop down for the create tab under the field set User Roles
            //First parenthesis is for create, the second is for modify
            DataSet local;
            if ((chkbxCSubunitClrk.Checked || chkbxMSubunitClrk.Checked) || (ddlRoles.SelectedItem.Text == "Subunit Clerk"))
            {
                local = presentation.GetStoresByType(2);
            }
            else
            {
                local = presentation.GetStoresByType(1);
            }

            ddlCStore.DataSource = local.Tables[1];
            ddlCStore.DataTextField = "storeName";
            ddlCStore.DataValueField = "storeID";
            ddlCStore.DataBind();

            divCStore.Style.Value = "visibility:hidden;";

            //load up the store names drop down for the modified tab under the field set User Roles

            ddlMStore.DataSource = local.Tables[1];
            ddlMStore.DataTextField = "storeName";
            ddlMStore.DataValueField = "storeID";
            ddlMStore.DataBind();
        }

        /// <summary>
        /// Method resets all the form fields for the designated tab
        /// </summary>
        /// <param name="tabFormType">string - tab name for form to be reset, valid types: 'create' or 'modify'</param>
        public void resetForm(string tabFormType)
        {
            //reset create tab form fields to initial state to allow for another possible user creation.
            if (tabFormType.Equals("create"))
            {
                txtCUserName.Text = "";
                txtCFirstName.Text = "";
                txtCLastName.Text = "";
                txtCPassword.Text = "";
                txtCPasswordHint.Text = "";
                //txtCEmail.Text = "";
                chkbxCAdmin.Checked = false;
                chkbxCGenMngr.Checked = false;
                chkbxCStoreMngr.Checked = false;
                //chkbxCStoreKpr.Checked = false;
                chkbxCSubunitClrk.Checked = false;
                chkbxCCeo.Checked = false;
                //chkbxCAccounting.Checked = false;
                chkbxCProcurement.Checked = false;
                divCStore.Style.Value = "visibility:hidden;";

                //reset the modify user form fields, so that it forces the user to select a new to reflect new changes.
                resetForm("modify");
            }
            //reset all store create fields
            else if (tabFormType.Equals("createS"))
            {
                txtCStoreName.Text = "";
                ddlStoreTypes.SelectedIndex = -1;

                //repopulate the store drop down for the store unit creation
                ds = presentation.GetActiveCentralStores();
                ddlCStores.DataSource = ds.Tables[1];
                ddlCStores.DataTextField = "storeName";
                ddlCStores.DataValueField = "storeID";
                ddlCStores.DataBind();

                //repopulate the modify/create user hidden 'pertaining store' drop downs, since there has been changes made.
                populateCStore_MStoreDropDowns();

                //reset the create user and modify user form tabs in order to compensate for the changed stores.
                resetForm("create");
                resetForm("modify");
            }
            //reset all sub unit create fields
            else if (tabFormType.Equals("createSB"))
            {
                txtCSubUnitName.Text = "";

                //repopulate the sub unit drop down for the store unit creation
                ds = presentation.GetActiveSubUnits();
                ddlCSubUnits.DataSource = ds.Tables[1];
                ddlCSubUnits.DataTextField = "subunitName";
                ddlCSubUnits.DataValueField = "subunitID";
                ddlCSubUnits.DataBind();
            }
            //reset all store unit create fields
            else if (tabFormType.Equals("createSU"))
            {
                ddlCStores.SelectedIndex = -1;
                ddlCSubUnits.SelectedIndex = -1;
            }
            //reset modify tab form fields to initial state to allow for another possible user account modification.
            else if (tabFormType.Equals("modify"))
            {
                txtMUserName.Text = "";
                txtMFirstName.Text = "";
                txtMLastName.Text = "";
                txtMPassword.Text = "";
                txtMPasswordHint.Text = "";
                //txtMEmail.Text = "";
                chkbxMAdmin.Checked = false;
                chkbxMGenMngr.Checked = false;
                chkbxMStoreMngr.Checked = false;
                //chkbxMStoreKpr.Checked = false;
                chkbxMSubunitClrk.Checked = false;
                chkbxMCeo.Checked = false;
                //chkbxMAccounting.Checked = false;
                chkbxMProcurement.Checked = false;
                ddlMStatus.SelectedIndex = -1;
                ddlMStore.SelectedIndex = -1;
                ddlRoles.SelectedIndex = -1;
                ddlRoles.Enabled = false;
                ddlStatuses.SelectedIndex = -1;
                ddlSearchBy.SelectedIndex = -1;
                ddlStatuses.Enabled = false;
                ddlUserList.SelectedIndex = -1;
                ddlUserList.Enabled = false;

                //disable all form fields
                txtMFirstName.Enabled = false;
                txtMLastName.Enabled = false;
                txtMUserName.Enabled = false;
                txtMPassword.Enabled = false;
                txtMPasswordHint.Enabled = false;
                ddlMStatus.Enabled = false;
                divMStore.Style.Value = "visibility:hidden";
                chkbxMAdmin.Enabled = false;
                chkbxMGenMngr.Enabled = false;
                chkbxMStoreMngr.Enabled = false;
                //chkbxMStoreKpr.Enabled = false;
                chkbxMSubunitClrk.Enabled = false;
                chkbxMCeo.Enabled = false;
                //chkbxMAccounting.Enabled = false;
                chkbxMProcurement.Enabled = false;
                //txtMEmail.Enabled = false;
                btnSavePassword.Enabled = false;
                btnSubmitChanges.Enabled = false;
            }
            //reset all store modify fields
            else if (tabFormType.Equals("modifyS"))
            {
                hfStoreID.Value = "";
                txtMStoreName.Text = "";
                ddlMStoreTypes.SelectedIndex = -1;
                ddlMStoreStatuses.SelectedIndex = -1;

                //disable all form fields
                txtMStoreName.Enabled = false;
                ddlMStoreTypes.Enabled = false;
                ddlMStoreStatuses.Enabled = false;
                btnSubmitStoreChanges.Enabled = false;

                //repopulate the modify/create user hidden 'pertaining store' drop downs, since there has been changes made.
                populateCStore_MStoreDropDowns();

                //reset the create user and modify user form tabs in order to compensate for the changed stores.
                resetForm("create");
                resetForm("modify");

                //repopulate the store list drop down with the new changes
                ds = presentation.GetAllStores();
                ddlStoreList.DataSource = ds.Tables[1];
                ddlStoreList.DataTextField = "storeName";
                ddlStoreList.DataValueField = "storeID";
                ddlStoreList.DataBind();

                //repopulate the store drop down with the new changes for the 'modify store unit' section
                ds = presentation.GetActiveCentralStores();
                ddlMStores.DataSource = ds.Tables[1];
                ddlMStores.DataTextField = "storeName";
                ddlMStores.DataValueField = "storeID";
                ddlMStores.DataBind();
            }
            //reset all sub unit modify fields
            else if (tabFormType.Equals("modifySB"))
            {
                hfSubUnitID.Value = "";
                txtMSubUnitName.Text = "";
                ddlMSubUnitStatuses.SelectedIndex = -1;

                //disable all form fields
                txtMSubUnitName.Enabled = false;
                ddlMSubUnitStatuses.Enabled = false;
                btnSubmitSubUnitChanges.Enabled = false;

                //repopulate sub unit list drop down
                ds = presentation.GetSubUnits();
                ddlSubUnitList.DataSource = ds.Tables[1];
                ddlSubUnitList.DataTextField = "subUnitName";
                ddlSubUnitList.DataValueField = "subUnitID";
                ddlSubUnitList.DataBind();

                //repopulate the sub unit drop down with the new changes for the 'modify store unit' section
                ds = presentation.GetActiveSubUnits();
                ddlMSubUnits.DataSource = ds.Tables[1];
                ddlMSubUnits.DataTextField = "subunitName";
                ddlMSubUnits.DataValueField = "subunitID";
                ddlMSubUnits.DataBind();
            }
            //reset all store unit modify fields
            else if (tabFormType.Equals("modifySU"))
            {
                //hfStoreUnitID.Value = "";
                ddlMStores.SelectedIndex = -1;
                ddlMSubUnits.SelectedIndex = -1;
                ddlMStoreUnitStatuses.SelectedIndex = -1;

                //disable all form fields
                ddlMStoreUnitStatuses.Enabled = false;
                btnSubmitStoreUnitChanges.Enabled = false;

                //repopulate the sub unit list drop down with the new changes
                ds = presentation.GetActiveSubUnits();
                ddlMSubUnits.DataSource = ds.Tables[1];
                ddlMSubUnits.DataTextField = "subunitName";
                ddlMSubUnits.DataValueField = "subunitID";
                ddlMSubUnits.DataBind();

                //repopulate the central store list drop down with the new changes.
                ds = presentation.GetActiveCentralStores();
                ddlMStores.DataSource = ds.Tables[1];
                ddlMStores.DataTextField = "storeName";
                ddlMStores.DataValueField = "storeID";
                ddlMStores.DataBind();
            }
        }
        #endregion

        #region Store Methods

        /// <summary>
        /// Method handles the select store button, for the modify store section.
        /// Populates the store fields according to the store selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnStoreSelect_Click(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for drop down selection.
            lblMStoreSuccess.Text = "";
            lblMStoreError.Text = "";

            //Try to pull the selected store's information from the database.
            ds = presentation.GetStoreByID(Convert.ToInt32(ddlStoreList.SelectedItem.Value));

            //counter any errors if any arise, otherwise populate modify user form fields according to returned results.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    //Populate the "Modify Store" form fields with the database results
                    hfStoreID.Value = ds.Tables[1].Rows[0][0].ToString();
                    txtMStoreName.Text = ds.Tables[1].Rows[0][1].ToString();
                    ddlMStoreTypes.SelectedValue = ds.Tables[1].Rows[0][2].ToString();
                    ddlMStoreStatuses.SelectedValue = ds.Tables[1].Rows[0][3].ToString();

                    //Enable modify store section form fields
                    txtMStoreName.Enabled = true;
                    ddlMStoreTypes.Enabled = true;
                    ddlMStoreStatuses.Enabled = true;
                    btnSubmitStoreChanges.Enabled = true;
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not find the store."; // If the Data Tier cannot be reached clear out any success or error messages to prepare for drop down selection.

        }
        /// <summary>
        /// Method handles the select sub unit button, for the modify sub unit section.
        /// Populates the sub unit fields according to the sub unit selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubUnitSelect_Click(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for drop down selection.
            lblMStoreSuccess.Text = "";
            lblMStoreError.Text = "";

            //Try to pull the selected store's information from the database.
            ds = presentation.GetSubUnitByID(Convert.ToInt32(ddlSubUnitList.SelectedItem.Value));

            //counter any errors if any arise, otherwise populate modify user form fields according to returned results.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    //Populate the "Modify Store" form fields with the database results
                    hfSubUnitID.Value = ds.Tables[1].Rows[0][0].ToString();
                    txtMSubUnitName.Text = ds.Tables[1].Rows[0][1].ToString();
                    ddlMSubUnitStatuses.SelectedValue = ds.Tables[1].Rows[0][2].ToString();

                    //Enable modify sub unit section form fields
                    txtMSubUnitName.Enabled = true;
                    ddlMSubUnitStatuses.Enabled = true;
                    btnSubmitSubUnitChanges.Enabled = true;
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not find the sub unit."; // If the Data Tier cannot be reached clear out any success or error messages to prepare for drop down selection.
        }
        /// <summary>
        /// Method handles the select store unit button, for the modify store unit section.
        /// Populates the store unit fields according to the store unit selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnStoreUnitSelect_Click(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for drop down selection.
            lblMStoreSuccess.Text = "";
            lblMStoreError.Text = "";

            //Try to pull the selected store's information from the database.
            ds = presentation.GetStoreUnitStatus(Convert.ToInt32(ddlMStores.SelectedItem.Value), Convert.ToInt32(ddlMSubUnits.SelectedItem.Value));

            //counter any errors if any arise, otherwise populate modify user form fields according to returned results.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    //Populate the "Modify Store" form fields with the database results
                    ddlMStoreUnitStatuses.SelectedValue = ds.Tables[1].Rows[0][0].ToString();

                    //Enable modify store section form fields
                    ddlMStoreUnitStatuses.Enabled = true;
                    btnSubmitStoreUnitChanges.Enabled = true;
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not find the store unit."; // If the Data Tier cannot be reached clear out any success or error messages to prepare for drop down selection.
        }
        /// <summary>
        /// Method handles the event of the save button
        /// for the Create Store section.
        /// Attempts to create a new store, depending on it's type, in the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveStore_Click(object sender, EventArgs e)
        {
            //Clear out the error and success labels before attempting the database changes.
            lblCStoreError.Text = "";
            lblCStoreSuccess.Text = "";

            //try to add the user
            ds = presentation.AddStore(txtCStoreName.Text, Convert.ToInt32(ddlStoreTypes.SelectedItem.Value));
            //counter any errors if any arise, otherwise create the store and reset the 'Create Store' form fields to their initial state.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    if (Convert.ToBoolean(ds.Tables[1].Rows[0][0].ToString()))
                    {
                        lblCStoreSuccess.Text = "Store \"" + txtCStoreName.Text + "\" added successfully.";
                        //Reset 'Create Store' form elements
                        resetForm("createS");
                    }
                    else
                        lblMasterError.Text = "Sorry, the store could not be added.";
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not add the store."; // If the Data Tier cannot be reached
        }
        /// <summary>
        /// Method handles the event of the save button for 
        /// the Create Sub Unit section.
        /// Attempts to create a new sub unit in the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveSubUnit_Click(object sender, EventArgs e)
        {
            //Clear out the error and success labels before attempting the database changes.
            lblCStoreError.Text = "";
            lblCStoreSuccess.Text = "";

            //try to add the user
            ds = presentation.AddSubUnit(txtCSubUnitName.Text);
            //counter any errors if any arise, otherwise create the Sub Unit and reset the 'Create Sub Unit' form fields to their initial state.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    if (Convert.ToBoolean(ds.Tables[1].Rows[0][0].ToString()))
                    {
                        lblCStoreSuccess.Text = "Sub unit \"" + txtCSubUnitName.Text + "\" added successfully.";
                        //Reset 'Create Sub unit' form elements
                        resetForm("createSB");
                    }
                    else
                        lblMasterError.Text = "Sorry, the sub unit could not be added.";
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not add the sub unit."; // If the Data Tier cannot be reached
        }
        /// <summary>
        /// Method handles the save button for the Create Store Unit section.
        /// Attempts to create a new store unit in the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveStoreUnit_Click(object sender, EventArgs e)
        {
            //Clear out the error and success labels before attempting the database changes.
            lblCStoreError.Text = "";
            lblCStoreSuccess.Text = "";

            //try to add the store unit

            ds = presentation.AddStoreUnit(Convert.ToInt32(ddlCStores.SelectedItem.Value), Convert.ToInt32(ddlCSubUnits.SelectedItem.Value));
            //counter any errors if any arise, otherwise create the store unit and reset the 'Create Store Unit' form fields to their initial state.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    if (Convert.ToBoolean(ds.Tables[1].Rows[0][0].ToString()))
                    {
                        lblCStoreSuccess.Text = "Store unit added successfully.";
                        //Reset 'Create Store unit' form elements
                        resetForm("createSU");
                    }
                    else
                        lblMasterError.Text = "Sorry, the store unit could not be added.";
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not add the store unit."; // If the Data Tier cannot be reached
        }
        /// <summary>
        /// Method handles the event of the submit changes button
        /// for the Modify Store section.
        /// Saves the modified store information.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmitStoreChanges_Click(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for database attempted changes.
            lblMStoreSuccess.Text = "";
            lblMStoreError.Text = "";

            //try to modify/update the store's info
            ds = presentation.ModifyStore(Convert.ToInt32(hfStoreID.Value), txtMStoreName.Text, Convert.ToInt32(ddlMStoreTypes.SelectedItem.Value), Convert.ToInt32(ddlMStoreStatuses.SelectedItem.Value));

            //counter any errors if any arise, otherwise modify/update the store's information and reset the 'Modify Store' form fields to their initial state.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    if (Convert.ToBoolean(ds.Tables[1].Rows[0][0].ToString()))
                    {
                        lblMStoreSuccess.Text = "Store \"" + txtMStoreName.Text + "\" updated successfully.";

                        //Reset 'Modify Store' form fields
                        resetForm("modifyS");
                    }
                    else
                        lblMasterError.Text = "Sorry, the store could not be updated.";
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not update the store."; // If the Data Tier cannot be reached
        }
        /// <summary>
        /// Method handles the event of the submit changes button
        /// for the Modify Sub Unit section.
        /// Saves the modified sub unit's information.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmitSubUnitChanges_Click(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for database attempted changes.
            lblMStoreSuccess.Text = "";
            lblMStoreError.Text = "";

            //try to modify/update the sub unit's info
            ds = presentation.ModifySubUnit(Convert.ToInt32(hfSubUnitID.Value), txtMSubUnitName.Text, Convert.ToInt32(ddlMSubUnitStatuses.SelectedItem.Value));

            //counter any errors if any arise, otherwise modify/update the sub unit's information and reset the 'Modify Sub Unit' form fields to their initial state.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    if (Convert.ToBoolean(ds.Tables[1].Rows[0][0].ToString()))
                    {
                        lblMStoreSuccess.Text = "Sub unit \"" + txtMSubUnitName.Text + "\" updated successfully.";

                        //Reset 'Modify Sub Unit' form fields
                        resetForm("modifySB");
                    }
                    else
                        lblMasterError.Text = "Sorry, the sub unit could not be updated.";
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not update the sub unit."; // If the Data Tier cannot be reached
        }
        /// <summary>
        /// Method handles the event of the submit changes button 
        /// in the Modify Store Unit section.
        /// Saves the modified store unit's information.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmitStoreUnitChanges_Click(object sender, EventArgs e)
        {
            //clear out any success or error messages to prepare for database attempted changes.
            lblMStoreSuccess.Text = "";
            lblMStoreError.Text = "";

            //try to modify/update the store unit's info
            ds = presentation.ModifyStoreUnit(Convert.ToInt32(ddlMStores.SelectedItem.Value), Convert.ToInt32(ddlMSubUnits.SelectedItem.Value), Convert.ToInt32(ddlMStoreUnitStatuses.SelectedItem.Value));

            //counter any errors if any arise, otherwise modify/update the store unit's information and reset the 'Modify Store Unit' form fields to their initial state.
            if (ds.Tables.Count > 0) // If the method returned successfully
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "") // if errors have occurred
                {
                    lblMasterError.Text = ds.Tables[0].Rows[0][0].ToString();
                }
                else
                {
                    if (Convert.ToBoolean(ds.Tables[1].Rows[0][0].ToString()))
                    {
                        lblMStoreSuccess.Text = "Store unit updated successfully.";

                        //Reset 'Modify Store Unit' form fields
                        resetForm("modifySU");
                    }
                    else
                        lblMasterError.Text = "Sorry, the store unit could not be updated.";
                }
            }
            else
                lblMasterError.Text = "Sorry, the system could not update the store."; // If the Data Tier cannot be reached
        }

        #endregion

        #region Session Methods
        private int GetStoreID()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getStoreId();
        }
        private string GetSessionID()
        {
            try
            {
                User lg = (User)Session["LoggedInUser"];
                return lg.getSessionId();
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return null;
            }
        }
        private string GetFirstName()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getFirstName();
        }
        private string GetLastName()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getLastName();
        }
        private bool GetIsAdmin()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAdmin();
        }
        private bool GetIsGeneralManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsGeneralManager();
        }
        private bool GetIsStoreManager()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreManager();
        }
        private bool GetIsStoreKeeper()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsStoreKeeper();
        }
        private bool GetIsSubUnitClerk()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsSubUnitClerk();
        }
        private bool GetIsAccounting()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsAccounting();
        }
        private bool GetIsProcurement()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsProcurement();
        }
        private bool GetIsCEO()
        {
            User lg = (User)Session["LoggedInUser"];
            return lg.getIsCEO();
        }
        #endregion
    }
}